package java基础知识;

/**
 * @author coderFu
 * @create 2022/2/7
 * 使用ThreadLocal修复双重检测
 */
public class ThreadLocalFinalSingleton {

    private static final ThreadLocal perThreadInstance = new ThreadLocal<>();
    private static ThreadLocalFinalSingleton singleton;

    private ThreadLocalFinalSingleton(){

    }

    //意思是只要有人进去过，那么其他人就可以不用进去了，直接获取值就可以。

    public static ThreadLocalFinalSingleton getInstance(){
        if (perThreadInstance.get() == null){
            createInstance();
        }
        return singleton;
    }

    private static final void createInstance(){
        synchronized (ThreadLocalFinalSingleton.class){
            if (singleton == null){
                singleton = new ThreadLocalFinalSingleton();
            }
            perThreadInstance.set(perThreadInstance);
        }
    }






}
